package com.etc.datefrme

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType}
import org.apache.spark.sql.{DataFrame, Row, SparkSession}

object sparksql2 {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().appName("hello").master("local").getOrCreate()
    val lines :RDD[String] = spark.sparkContext.textFile("e:\\a.txt")

//    val schemaString = "name age"
//    val fields = schemaString.split(" ").map(fieldName =>StructField(fieldName,StringType,nullable = true))
//    val schema = StructType(fields)

    val structType = StructType(Array(
      StructField("name", StringType,true),
      StructField("age", IntegerType,true)
    ))


    val rowRDD = lines.map(_.split(" ")).map(attributes =>Row(attributes(0),attributes(1).toInt))

    val frame :DataFrame= spark.createDataFrame(rowRDD,structType)
    frame.createOrReplaceTempView("person")

    spark.sql("select * from person").show()

    spark.stop()

  }

}
